<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  
  
  <link rel="shortcut icon" href="img/favicon.ico">
  <title>Reflect - Java Note</title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700" />

  <link rel="stylesheet" href="css/theme.css" />
  <link rel="stylesheet" href="css/theme_extra.css" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css" />
  
  <script>
    // Current page data
    var mkdocs_page_name = "Reflect";
    var mkdocs_page_input_path = "Reflect.md";
    var mkdocs_page_url = null;
  </script>
  
  <script src="js/jquery-2.1.1.min.js" defer></script>
  <script src="js/modernizr-2.8.3.min.js" defer></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script> 
  
</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
    <div class="wy-side-scroll">
      <div class="wy-side-nav-search">
        <a href="." class="icon icon-home"> Java Note</a>
        <div role="search">
  <form id ="rtd-search-form" class="wy-form" action="./search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" title="Type search term here" />
  </form>
</div>
      </div>

      <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="index.html">Home</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="StringX.html">StringX</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Collection.html">Collection</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="IO.html">IOFile</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Lambda.html">Lambda</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Generic.html">Generic</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Stream.html">Stream</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Atomic.html">Atomic</a>
                    </li>
                </ul>
                <ul class="current">
                    <li class="toctree-l1 current"><a class="reference internal current" href="Reflect.html">Reflect</a>
    <ul class="current">
    <li class="toctree-l2"><a class="reference internal" href="#class">Class 类</a>
        <ul>
    <li class="toctree-l3"><a class="reference internal" href="#class_1">得到 Class 对象</a>
    </li>
        </ul>
    </li>
    <li class="toctree-l2"><a class="reference internal" href="#member">Member</a>
        <ul>
    <li class="toctree-l3"><a class="reference internal" href="#java">Java 权限检测</a>
    </li>
    <li class="toctree-l3"><a class="reference internal" href="#field">Field</a>
        <ul>
    <li class="toctree-l4"><a class="reference internal" href="#set">Set</a>
    </li>
        </ul>
    </li>
    <li class="toctree-l3"><a class="reference internal" href="#method">Method</a>
    </li>
    <li class="toctree-l3"><a class="reference internal" href="#parameter">Parameter</a>
    </li>
        </ul>
    </li>
    <li class="toctree-l2"><a class="reference internal" href="#constructor">Constructor</a>
        <ul>
    <li class="toctree-l3"><a class="reference internal" href="#_2">创造对象</a>
    </li>
        </ul>
    </li>
    <li class="toctree-l2"><a class="reference internal" href="#_3">数组和枚举</a>
        <ul>
    <li class="toctree-l3"><a class="reference internal" href="#_4">数组</a>
        <ul>
    <li class="toctree-l4"><a class="reference internal" href="#_5">创建和初始化</a>
    </li>
        </ul>
    </li>
    <li class="toctree-l3"><a class="reference internal" href="#_6">枚举</a>
    </li>
        </ul>
    </li>
    <li class="toctree-l2"><a class="reference internal" href="#_7">注解</a>
    </li>
    <li class="toctree-l2"><a class="reference internal" href="#others">Others</a>
    </li>
    </ul>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="JavaFX.html">JavaFX</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="Sundry.html">Sundry</a>
                    </li>
                </ul>
      </div>
    </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href=".">Java Note</a>
      </nav>

      
      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href=".">Docs</a> &raquo;</li>
    
      
    
    <li>Reflect</li>
    <li class="wy-breadcrumbs-aside">
      
    </li>
  </ul>
  
  <hr/>
</div>
          <div role="main">
            <div class="section">
              
                <h2 id="_1">反射</h2>
<blockquote>
<p>Java反射机制是在运行状态中<br />
  1. 对于任意一个类，都能知道这个类的所以属性和方法
  2. 对于任何一个对象，都能够调用它的任何一个方法和属性
这样动态获取新的以及动态调用对象方法的功能就叫做反射</p>
</blockquote>
<h3 id="class">Class 类</h3>
<blockquote>
<p>Class 是 反射实现的基础 <code>java.lang.Class</code></p>
<p>对于每一种类 Java虚拟机都会初始化出一个Class类型的实例 <br />
每当编写并且编译一个新创建的类就会产生一个对应Class对象
并且会被保存在同名.class文件</p>
</blockquote>
<p>当new一个新对象或者引用静态成员变量时 Java虚拟机(JVM)中的类加载器系统会将对应Class对象加载到JVM中 然后JVM再根据这个类型信息相关的Class对象创建我们需要实例对象或者提供静态变量的引用值</p>
<p><strong>构造器是私有的 只有JVM才可以调用这个构造函数创建Class的对象</strong></p>
<blockquote>
<p>Class是反射能够实现的基础的另一个原因是：Java反射包 <code>java.lang.reflect</code> 中的所有类都没有 <code>public</code> 构造方法<br />
要想获得这些类实例 只能通过Class类获取<br />
所以说如果想使用反射 必须得获得Class对象</p>
</blockquote>
<h4 id="class_1">得到 Class 对象</h4>
<ol>
<li>通过对象实例获取对应Class对象 <code>Object.getClass()</code><blockquote>
<p>invalid for basic type</p>
</blockquote>
</li>
</ol>
<pre><code class="language-java">//Returns the Class for String
Class c = &quot;foo&quot;.getClass();

enum E { A, B }
//Returns the Class corresponding to the enumeration type E.
Class c = A.getClass();

byte[] bytes = new byte[1024];
//Returns the Class corresponding to an array with component type byte.
Class c = bytes.getClass();

Set&lt;String&gt; s = new HashSet&lt;String&gt;();
//Returns the Class corresponding to java.util.HashSet.
Class c = s.getClass();
</code></pre>
<ol>
<li>通过类的类型获取Class对象 <blockquote>
<p>基本类型同样可以使用这种方法</p>
</blockquote>
</li>
</ol>
<pre><code class="language-java">//The `.class` syntax returns the Class corresponding to the type `boolean`.
Class c = boolean.class;  

//Returns the Class for String
Class c = String.class;
</code></pre>
<ol>
<li>通过类的全限定名获取Class对象<blockquote>
<p>基本类型无法使用此方法
数组的支持可能有问题</p>
</blockquote>
</li>
</ol>
<pre><code class="language-java">Class c = Class.forName(&quot;java.lang.String&quot;);//通过Class.forName()方法加载的类，采用的是系统类加载器
//对于数组比较特殊
Class cDoubleArray = Class.forName(&quot;[D&quot;);    //相当于double[].class

Class cStringArray = Class.forName(&quot;[[Ljava.lang.String;&quot;);   //相当于String[][].class
</code></pre>
<ol>
<li>基本类型和 void 类型的包装类可以使用TYPE字段获取</li>
</ol>
<pre><code class="language-java">Class c = Double.TYPE;   //等价于 double.class.

Class c = Void.TYPE;
</code></pre>
<ol>
<li>一些其他办法</li>
</ol>
<pre><code class="language-java">Class.getSuperclass()//获得给定类的父类Class
Class.getClasses()
Class.getDeclaredClasses()
Class.getDeclaringClass()
Class.getEnclosingClass()
java.lang.reflect.Field.getDeclaringClass()
java.lang.reflect.Method.getDeclaringClass()
java.lang.reflect.Constructor.getDeclaringClass()
</code></pre>
<ol>
<li>一般</li>
</ol>
<pre><code class="language-java">Class c1 = Test.class; //这说明任何一个类都有一个隐含的静态成员变量class，这种方式是通过获取类的静态成员变量class得到的()
Class c2 = test.getClass();// test是Test类的一个对象，这种方式是通过一个类的对象的getClass()方法获得的 (对于基本类型无法使用这种方法)
Class c3 = Class.forName(&quot;com.catchu.me.reflect.Test&quot;); //这种方法是Class类调用forName方法，通过一个类的全量限定名获得（基本类型无法使用此方法）
</code></pre>
<h3 id="member">Member</h3>
<blockquote>
<p>Reflection defines an interface <code>java.lang.reflect.Member</code> which is implemented by 
<code>java.lang.reflect.Field</code> <code>java.lang.reflect.Method</code>  and <code>java.lang.reflect.Constructor</code></p>
</blockquote>
<p><code>java.lang.reflect.Field</code>       : 对应类变量<br />
<code>java.lang.reflect.Method</code>      : 对应类方法<br />
<code>java.lang.reflect.Constructor</code> : 对应类构造函数  </p>
<blockquote>
<p>反射就是通过这三个类才能在运行时改变对象状态</p>
</blockquote>
<h4 id="java">Java 权限检测</h4>
<blockquote>
<p>Java运行时会进行访问权限检查，private类型的变量无法进行直接访问</p>
</blockquote>
<p><code>java.lang.reflect.AccessibleObject</code> 提供一个方法 <code>setAccessible(boolean flag)</code> 可以取消Java 语言访问权限检查</p>
<blockquote>
<p><strong>所以任何继承<a href="https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/AccessibleObject.html"><code>AccessibleObject</code></a>的类的对象都可以使用该方法取消 Java 语言访问权限检查。（final类型变量也可以通过这种办法访问）</strong></p>
</blockquote>
<pre><code class="language-java">public final class Field extends AccessibleObject implements Member
//Field Method Constructor 都是继承 AccessibleObject
</code></pre>
<pre><code class="language-java">public class Cat {//测试类
    public static final String TAG = Cat.class.getSimpleName();
    private String name;
    @Deprecated
    public int age;

    public Cat(String name, int age){
        this.name = name;
        this.age = age;
    }

    public String getName(){
        return name;
    }

    public void eat(String food){
        Log.d(TAG, &quot;eat food &quot; + food);
    }

    public void eat(String... foods){
        StringBuilder s = new StringBuilder();
        for(String food : foods){
            s.append(food);
            s.append(&quot; &quot;);
        }
        Log.d(TAG, &quot;eat food &quot; + s.toString());
    }

    public void sleep(){
        Log.d(TAG, &quot;sleep&quot;);
    }

    @Override
    public String toString() {
        return &quot;name = &quot; + name + &quot; age = &quot; + age;
    }
}
</code></pre>
<h4 id="field">Field</h4>
<p>通过Field你可以访问给定对象的类变量，包括获取变量的<strong>类型、修饰符、注解、变量名、变量的值或者重新设置变量值</strong> 即使变量是private的<br />
<strong>获取Field</strong>
Class提供了4种方法获得给定类的Field
<code>getDeclaredField(String name)</code>
获取指定的变量（只要是声明的变量都能获得，包括private）
<code>getField(String name)</code>
获取指定的变量（只能获得public的）
<code>getDeclaredFields()</code>
获取所有声明的变量（包括private）
<code>getFields()</code>
获取所有的public变量
<strong>获取变量类型、修饰符、注解</strong></p>
<p>返回类型都是 <code>Field[]</code></p>
<pre><code class="language-java">Field[] fields=__Class__.getDeclaredFields();
fields.getName();
fields.getType();
fields.getAnnotations();//Type= Annotation[]
</code></pre>
<pre><code class="language-java">public void testField(){
        Class c = Cat.class;//Type
        Field[] fields = c.getDeclaredFields();//Fields get
        for(Field f : fields){
            StringBuilder builder = new StringBuilder();
            //获取名称
            builder.append(&quot;filed name = &quot;);
            builder.append(f.getName());
            //获取类型
            builder.append(&quot; type = &quot;);
            builder.append(f.getType());
            //获取修饰符
            builder.append(&quot; modifiers = &quot;);
            builder.append(Modifier.toString(f.getModifiers()));
            //获取注解
            Annotation[] ann = f.getAnnotations();
            if (ann.length != 0) {
                builder.append(&quot; annotations = &quot;);
                for (Annotation a : ann){
                    builder.append(a.toString());
                    builder.append(&quot; &quot;);
                }
            } else {
                builder.append(&quot;  -- No Annotations --&quot;);
            }
            Log.d(TAG, builder.toString());
        }
    }
</code></pre>
<h5 id="set">Set</h5>
<pre><code class="language-java">public void testField(){
        Cat cat = new Cat(&quot;Tom&quot;, 2);
        Class c = cat.getClass();
        try {
            //注意获取private变量时，需要用getDeclaredField
            Field fieldName = c.getDeclaredField(&quot;name&quot;);//获得了成员,但不知道是哪个类的
            Field fieldAge = c.getField(&quot;age&quot;);
             fieldName.setAccessible(true);
            //反射获取名字, 年龄
            String name = (String) fieldName.get(cat);//实际获得数据
            int age = fieldAge.getInt(cat);
            Log.d(TAG, &quot;before set, Cat name = &quot; + name + &quot; age = &quot; + age);
            //反射重新set名字和年龄
            fieldName.set(cat, &quot;Timmy&quot;);
            fieldAge.setInt(cat, 3);
            Log.d(TAG, &quot;after set, Cat &quot; + cat.toString());
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
</code></pre>
<h4 id="method">Method</h4>
<blockquote>
<p>获取Method
Class依然提供了4种方法获取Method:
<code>getDeclaredMethod(String name, Class&lt;?&gt;... parameterTypes)</code>
根据方法名获得指定的方法， 参数name为方法名，参数parameterTypes为方法的参数类型，如 <code>getDeclaredMethod(“eat”, String.class)</code>
<code>getMethod(String name, Class&lt;?&gt;... parameterTypes)</code>
根据方法名获取指定的public方法，其它同上
<code>getDeclaredMethods()</code>
获取所有声明的方法
<code>getMethods()</code>
获取所有的public方法</p>
</blockquote>
<table>
<thead>
<tr>
<th align="center">方法类型</th>
<th>作用</th>
<th>区别</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center"><code>getReturnType()</code></td>
<td>获取目标方法返回类型对应的Class对象</td>
<td>返回类型 Class</td>
</tr>
<tr>
<td align="center"><code>getGenericReturnType()</code></td>
<td>获取目标方法返回类型对应的Type对象</td>
<td>Type (Class 实现)</td>
</tr>
<tr>
<td align="center">&gt; 当返回值 为 普通简单类型 如 <code>Object</code> <code>int</code> <code>String</code> 等 没有区别</td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>类型</th>
<th><code>getReturnType()</code></th>
<th><code>getGenericReturnType()</code></th>
</tr>
</thead>
<tbody>
<tr>
<td><code>public T func()</code></td>
<td><code>class java.lang.Object</code></td>
<td><code>T</code></td>
</tr>
<tr>
<td><code>public Class&lt;String&gt; func()</code></td>
<td><code>class java.lang.Class</code></td>
<td><code>java.lang.Class&lt;java.lang.String&gt;</code></td>
</tr>
</tbody>
</table>
<h4 id="parameter">Parameter</h4>
<table>
<thead>
<tr>
<th>获取函数</th>
<th>作用</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getParameterTypes--"><code>getParameterTypes()</code></a></td>
<td>获取目标方法各参数类型对应的Class对象</td>
<td>参数类型 两者区别同上</td>
</tr>
<tr>
<td><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getGenericParameterTypes--"><code>getGenericParameterTypes()</code></a></td>
<td>获取目标方法各参数类型对应的Type对象<br/>返回值为数组</td>
<td></td>
</tr>
<tr>
<td><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getExceptionTypes--"><code>getExceptionTypes()</code></a></td>
<td>获取目标方法抛出的异常类型对应的Class对象</td>
<td></td>
</tr>
<tr>
<td><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getGenericExceptionTypes--"><code>getGenericExceptionTypes()</code></a></td>
<td>获取目标方法抛出的异常类型对应的Type对象<br/>返回值为数组</td>
<td></td>
</tr>
</tbody>
</table>
<blockquote>
<p>class文件中默认不存储方法参数名称，如果想要获取方法参数名称，需要在编译的时候加上<code>-parameters</code>参数</p>
</blockquote>
<pre><code class="language-java">//这里的m可以是普通方法Method，也可以是构造方法Constructor
//获取方法所有参数
Parameter[] params = m.getParameters();
for (int i = 0; i &lt; params.length; i++) {
    Parameter p = params[i];
    p.getType();   //获取参数类型
    p.getName();  //获取参数名称，如果编译时未加上`-parameters`，返回的名称形如`argX`, X为参数在方法声明中的位置，从0开始
    p.getModifiers(); //获取参数修饰符
    p.isNamePresent();  //.class文件中是否保存参数名称, 编译时加上`-parameters`返回true,反之flase
}
</code></pre>
<table>
<thead>
<tr>
<th>Method</th>
<th>Func</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>isVarArgs()</code></td>
<td>判断方法参数是否为可变参数</td>
</tr>
<tr>
<td><code>isSynthetic()</code></td>
<td>判断是否为复合/合成方法</td>
</tr>
<tr>
<td><code>isBridge()</code></td>
<td>判断是否是<a href="https://www.cnblogs.com/zsg88/p/7588929.html">桥接方法</a></td>
</tr>
</tbody>
</table>
<p>方法调用</p>
<blockquote>
<p>反射通过Method的<a href="https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#invoke-java.lang.Object-java.lang.Object...-"><code>invoke()</code></a>方法来调用目标方法。第一个参数为需要调用的<strong>目标类对象</strong>，如果方法为static的，则该参数为null。后面的参数都为目标方法的参数值，顺序与目标方法声明中的参数顺序一致</p>
</blockquote>
<pre><code class="language-java">public native Object invoke(Object obj, Object... args)
            throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
</code></pre>
<pre><code class="language-java">Class&lt;?&gt; c = Cat.class;
 try {
     //构造Cat实例
     Constructor constructor = c.getConstructor(String.class, int.class);
     Object cat = constructor.newInstance( &quot;Jack&quot;, 3);
     //调用无参方法
     Method sleep = c.getDeclaredMethod(&quot;sleep&quot;);
     sleep.invoke(cat);
     //调用定项参数方法
     Method eat = c.getDeclaredMethod(&quot;eat&quot;, String.class);
     eat.invoke(cat, &quot;grass&quot;);
     //调用不定项参数方法
     //不定项参数可以当成数组来处理
     Class[] argTypes = new Class[] { String[].class };
     Method varargsEat = c.getDeclaredMethod(&quot;eat&quot;, argTypes);
     String[] foods = new String[]{
          &quot;grass&quot;, &quot;meat&quot;
     };
     varargsEat.invoke(cat, (Object)foods);
  } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
     e.printStackTrace();
 }
</code></pre>
<h3 id="constructor">Constructor</h3>
<p>和Method一样，Class也为Constructor提供了4种方法获取
 <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getDeclaredConstructor-java.lang.Class...-"><code>getDeclaredConstructor(Class... parameterTypes)</code></a>
 获取指定构造函数，参数parameterTypes为构造方法的参数类型
 <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getConstructor-java.lang.Class...-"><code>getConstructor(Class... parameterTypes)</code></a>
 获取指定public构造函数，参数parameterTypes为构造方法的参数类型
 <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getDeclaredConstructors--"><code>getDeclaredConstructors()</code></a>
 获取所有声明的构造方法
 <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getConstructors--"><code>getConstructors()</code></a>
 获取所有的public构造方法</p>
<h4 id="_2">创造对象</h4>
<blockquote>
<p><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Constructor.html#newInstance-java.lang.Object...-"><code>java.lang.reflect.Constructor.newInstance()</code></a>
<a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#newInstance--"><code>Class.newInstance()</code></a></p>
</blockquote>
<p>区别：</p>
<table>
<thead>
<tr>
<th><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#newInstance--"><code>Class.newInstance()</code></a></th>
<th><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Constructor.html#newInstance-java.lang.Object...-"><code>Constructor.newInstance()</code></a></th>
</tr>
</thead>
<tbody>
<tr>
<td>仅可用来调用无参的构造方法</td>
<td>可以调用任意参数的构造方法</td>
</tr>
<tr>
<td>将构造方法中抛出的异常不作处理原样抛出</td>
<td>将构造方法中抛出的异常都包装成<a href="https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/InvocationTargetException.html"><code>InvocationTargetException</code></a>抛出</td>
</tr>
<tr>
<td>需要拥有构造方法的访问权限</td>
<td>可以通过<code>setAccessible(true)</code>方法绕过访问权限访问private构造方法</td>
</tr>
</tbody>
</table>
<blockquote>
<p><strong>反射不支持自动封箱</strong></p>
</blockquote>
<h3 id="_3">数组和枚举</h3>
<h4 id="_4">数组</h4>
<blockquote>
<p>数组本质上是一个对象</p>
<p>例如对于int[] intArray，数组类型为class [I。数组类型中的[个数代表数组的维度，例如[代表一维数组，[[代表二维数组；[后面的字母代表数组元素类型，I代表int，一般为类型的首字母大写(long类型例外，为J)</p>
</blockquote>
<pre><code class="language-java">class [B    //byte类型一维数组
class [S    //short类型一维数组
class [I    //int类型一维数组
class [C    //char类型一维数组
class [J    //long类型一维数组，J代表long类型，因为L被引用对象类型占用了
class [F    //float类型一维数组
class [D    //double类型一维数组
class [Lcom.dada.Season    //引用类型一维数组
class [[Ljava.lang.String  //引用类型二维数组
</code></pre>
<h5 id="_5">创建和初始化</h5>
<blockquote>
<p><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Array.html"><code>java.lang.reflect.Array</code></a> 来创建和初始化数组</p>
</blockquote>
<pre><code class="language-java">//创建数组， 参数componentType为数组元素的类型，后面不定项参数的个数代表数组的维度，参数值为数组长度
Array.newInstance(Class&lt;?&gt; componentType, int... dimensions);

//设置数组值，array为数组对象，index为数组的下标，value为需要设置的值
Array.set(Object array, int index, int value);

//获取数组的值，array为数组对象，index为数组的下标
Array.get(Object array, int index);


//获取一个变量的类型
Class&lt;?&gt; c = field.getType();
//判断该变量是否为数组
if (c.isArray()) {
   //获取数组的元素类型
   c.getComponentType();
}
</code></pre>
<h4 id="_6">枚举</h4>
<blockquote>
<p>枚举隐式继承自<a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Enum.html"><code>java.lang.Enum</code></a></p>
<p><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#isEnum--"><code>Class.isEnum()</code></a>
 Indicates whether this class represents an enum type
 <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getEnumConstants--"><code>Class.getEnumConstants()</code></a>
 Retrieves the list of enum constants defined by the enum in the order they're declared
 <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#isEnumConstant--"><code>java.lang.reflect.Field.isEnumConstant()</code></a>
 Indicates whether this field represents an element of an enumerated type</p>
</blockquote>
<h3 id="_7">注解</h3>
<pre><code class="language-java">Annotation[] annotations = (Annotation[]) class1.getAnnotations();//获取class对象的所有注解 
Annotation annotation = (Annotation) class1.getAnnotation(Deprecated.class);//获取class对象指定注解 
Type genericSuperclass = class1.getGenericSuperclass();//获取class对象的直接超类的 
Type Type[] interfaceTypes = class1.getGenericInterfaces();//获取class对象的所有接口的type集合
</code></pre>
<h3 id="others">Others</h3>
<pre><code class="language-java">boolean isPrimitive = class1.isPrimitive();//判断是否是基础类型 
boolean isArray = class1.isArray();//判断是否是集合类
boolean isAnnotation = class1.isAnnotation();//判断是否是注解类 
boolean isInterface = class1.isInterface();//判断是否是接口类 
boolean isEnum = class1.isEnum();//判断是否是枚举类 
boolean isAnonymousClass = class1.isAnonymousClass();//判断是否是匿名内部类 
boolean isAnnotationPresent = class1.isAnnotationPresent(Deprecated.class);//判断是否被某个注解类修饰 
String className = class1.getName();//获取class名字 包含包名路径 
Package aPackage = class1.getPackage();//获取class的包信息 
String simpleName = class1.getSimpleName();//获取class类名 
int modifiers = class1.getModifiers();//获取class访问权限 
Class&lt;?&gt;[] declaredClasses = class1.getDeclaredClasses();//内部类 
Class&lt;?&gt; declaringClass = class1.getDeclaringClass();//外部类
ClassLoader ClassLoader = class1.getClassLoader() 返回类加载器

getSuperclass()：获取某类所有的父类  
getInterfaces()：获取某类所有实现的接口
</code></pre>
<p><a href="https://www.jianshu.com/p/10c29883eac1">Reference</a></p>
              
            </div>
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="JavaFX.html" class="btn btn-neutral float-right" title="JavaFX">Next <span class="icon icon-circle-arrow-right"></span></a>
      
      
        <a href="Atomic.html" class="btn btn-neutral" title="Atomic"><span class="icon icon-circle-arrow-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <!-- Copyright etc -->
    
  </div>

  Built with <a href="https://www.mkdocs.org/">MkDocs</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
      
        </div>
      </div>

    </section>

  </div>

  <div class="rst-versions" role="note" aria-label="versions">
    <span class="rst-current-version" data-toggle="rst-current-version">
      
      
        <span><a href="Atomic.html" style="color: #fcfcfc;">&laquo; Previous</a></span>
      
      
        <span style="margin-left: 15px"><a href="JavaFX.html" style="color: #fcfcfc">Next &raquo;</a></span>
      
    </span>
</div>
    <script>var base_url = '.';</script>
    <script src="js/theme.js" defer></script>
      <script src="search/main.js" defer></script>
    <script defer>
        window.onload = function () {
            SphinxRtdTheme.Navigation.enable(true);
        };
    </script>

</body>
</html>
